#include <allegro.h>
#include "general.h"
#include <time.h>
#include <stdio.h>
#define RAND_NUM (rand()/RAND_MAX)

int bounding_box_collision(int sprite1_x, int sprite1_y, int sprite1_w, int sprite1_h, int sprite2_x, int sprite2_y, int sprite2_w, int sprite2_h);
int get_pixel(BITMAP* Buffer,int x,int y);
void gravity(float *y);
void gravity_mario(float *poz,codo *c,BITMAP * Background);
void jump_mario(float *poz,codo *c,BITMAP * Background);

void move_mario(float *poz,codo *c,BITMAP * Background);
int collision_Background_up(float *poz,codo *c,int sprite1_x,int sprite1_y,int sprite1_h,int sprite1_w,BITMAP* Background2);
int collision_Background_down(float *poz,int sprite1_x,int sprite1_y,int sprite1_h,int sprite1_w,BITMAP* Background2);
int collision_Background_right(float poz,codo c,BITMAP* Background2);
int collision_Background_left(float poz,codo c,BITMAP* Background2);
void timer_routine(int timer);

int main() 
{
int f=8;
int test=0;
int chois=0;	
char buf[50];
int timer=0;
//int pause=0;
//int temp_pause=0;
//time_t t,t1,t2,t3;
BITMAP *Buffer;
BITMAP *etoile;
BITMAP *Background;
BITMAP *Background2;
codo c ;
Ball b;
Monster o;
Monster o1;
Monster o2;
kartouche k;
power w;
BITMAP *menu;
Money m,m1,m2 ;
Etoile e,e1,e2;
int i;
int kar=0;
int score=0;
int compteur;
SAMPLE *sound;
allegro_init();
LOCK_VARIABLE(timer);
LOCK_FUNCTION(timer_routine);
//install_int_ex(timer_routine,BPS_TO_TIMER(60));

if (install_keyboard() == -1) {
allegro_message("Error! %s", allegro_error);
return -1;
}

set_color_depth(16);

if (set_gfx_mode(GFX_AUTODETECT_WINDOWED,1300,500, 0, 0) != 0) {
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); /* here’s the command for text mode */
allegro_message("Unable to initialize video mode!\n%s\n", allegro_error);
return 1;
}

compteur=0;
c.x=100;
c.y=150 ;
//c.power =100;
c.direction=0;

o.x=1100;
o.y=150;
o.n=0;

b.x=500;
b.y=400;
b.n=0;

o1.x=9000;
o1.y=300;
o1.n=0;


o2.x=5000;
o2.y=300;
o2.n=0;

m.x=400;
m.y=240;
m.n=0;

m1.x=5000;
m1.y=240;
m1.n=0;

m2.x=7000;
m2.y=240;
m2.n=0;


e2.x=8800;
e2.y=240;
e2.n=0;


e1.x=1200;
e1.y=240;
e1.n=0;

e.x=2400;
e.y=240;
e.n=0;

float poz=0;
sound = load_sample("sound.wav");
Background = load_bitmap("./img/background.bmp", NULL);
Background2 = load_bitmap("./img/background_collision.bmp", NULL);

menu = load_bitmap("./img/menu.bmp", NULL);
chargement (&c,&m,&e,&o,&o1,&o2,&k,&w,&b);

Buffer = create_bitmap(1300, 600);
blit(menu, Buffer, 0, 0, 0, 0, menu->w,menu->h);
blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W,SCREEN_H);
while (!key[KEY_S]) {
blit(menu, Buffer, 0, 0, 0, 0, menu->w,menu->h);
blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W,SCREEN_H);
textout_centre_ex(screen, font, "Start Game", 660,100, makecol(100, 255, 100), -1);
textout_centre_ex(screen, font, "Save", 200,50, makecol(100, 255, 100), -1);
textout_centre_ex(screen, font, "Exit Game",1100,50, makecol(100, 255, 100), -1);
}

///*Sound*///

if(install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL)) {
allegro_message("Sound Error: %s", allegro_error);

}

///////*Volume*////////////

set_volume(255, 255); //digital and music to max levels

///////*Load*///////////

sound = load_sample("sound.wav");
play_sample(sound, 255, 128, 1000, 0);
if (!sound)
{allegro_message("Error reading wav file"); }

Buffer = create_bitmap(14142, 500);
//time(&t1);
while (!key[KEY_ESC]) 
{

//if(key[KEY_R])
//{allegro_message("Partie reprise !");
//pause=0;
//time(&t3);
//}

//while(pause==0){
//time(&t);
//temp_pause = difftime(t,t1);
//if(key[KEY_P])
//{allegro_message("Pause !");
//time(&t2);
//pause=1;
//}


if (k.x>SCREEN_W || k.x<0)
k.x=c.x;
k.y=c.y+20;

blit(Background, Buffer, poz, 0, 0, 0, Background->w, Background->h);//copier le Background sur le buffer 

move_mario(&poz,&c,Background2);

//time(&t);

//if (pause==0)
//textprintf_centre_ex(Buffer, font, 100,50,makecol(0, 0 ,0), -1,"TIME : %0.0f",difftime(t,t1)-difftime(t3,t2));
//else
//textprintf_centre_ex(Buffer, font, 100,100,makecol(0, 0 ,0), -1,"TIME : %d",temp_pause);

//textprintf_centre_ex(Buffer, font, 110,50,makecol(0, 0 ,0), -1,"POWER : %d",c.power);
textprintf_centre_ex(Buffer,font,50,50,makecol(0,0,0), -1,"score : %d",score);
draw_sprite(Buffer,w.pm[f],50,0);


int sprite1_x=c.x;
int sprite1_y=c.y;
int sprite1_w=c.tab_d[0]->w;
int sprite1_h=c.tab_d[0]->h;

int sprite11_x=k.x;
int sprite11_y=k.y;
int sprite11_w=k.tab_d[0]->w;
int sprite11_h=k.tab_d[0]->h;

int sprite10_x=o.x;
int sprite10_y=o.y;
int sprite10_w=o.tab_g[0]->w;
int sprite10_h=o.tab_g[0]->h;

int sprite8_x=o1.x;
int sprite8_y=o1.y;
int sprite8_w=o1.tab_g[0]->w;
int sprite8_h=o1.tab_g[0]->h;


int sprite15_x=b.x;
int sprite15_y=b.y;
int sprite15_w=b.tab_d[0]->w;
int sprite15_h=b.tab_d[0]->h;


int sprite9_x=o2.x;
int sprite9_y=o2.y;
int sprite9_w=o2.tab_g[0]->w;
int sprite9_h=o2.tab_g[0]->h;

int sprite2_x=m.x;
int sprite2_y=m.y;
int sprite2_w=m.money->w;
int sprite2_h=m.money->h;

int sprite3_x=e.x;
int sprite3_y=e.y;
int sprite3_w=e.etoile->w;
int sprite3_h=e.etoile->h;

int sprite4_x=e1.x;
int sprite4_y=e1.y;
int sprite4_w=e.etoile->w;
int sprite4_h=e.etoile->h;

int sprite5_x=e2.x;
int sprite5_y=e2.y;
int sprite5_w=e.etoile->w;
int sprite5_h=e.etoile->h;

int sprite6_x=m1.x;
int sprite6_y=m1.y;
int sprite6_w=m.money->w;
int sprite6_h=m.money->h;

int sprite7_x=m2.x;
int sprite7_y=m2.y;
int sprite7_w=m.money->w;
int sprite7_h=m.money->h;
/*
if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,sprite11_x, sprite11_y, sprite11_w, sprite11_h )==1){
m.n=1;
c.power=c.power-20;
}


if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,sprite8_x, sprite8_y, sprite8_w, sprite8_h )==1){
o.n=1;
c.power=c.power-20;
}


if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,sprite9_x, sprite9_y, sprite9_w, sprite9_h )==1){
o.n=1;
c.power=c.power-20;
}

if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,sprite10_x, sprite10_y, sprite10_w, sprite10_h )==1){
o.n=1;
c.power=c.power-20;
}
*/
if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,sprite2_x, sprite2_y, sprite2_w, sprite2_h )==1){
m.n=1;
score=score+20;
m.x=-100;
m.y=-100;
}


if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,sprite15_x, sprite15_y, sprite15_w, sprite15_h )==1){
b.n=1;
score=score-10;
b.x=-1000;
b.y=-1000;
}


if(bounding_box_collision(sprite10_x, sprite10_y, sprite10_w, sprite10_h,sprite1_x, sprite1_y, sprite1_w, sprite1_h )==1){
score=score+100;
}

if(bounding_box_collision(sprite10_x, sprite10_y, sprite10_w, sprite10_h,sprite11_x, sprite11_y, sprite11_w, sprite11_h )==1){
//o.n=1;
score=score+100;
f--;
if (f==0) {
o.x=-10000000;
o.y=-10000000;
f=8;
}
k.x=c.x;
}

if(bounding_box_collision(sprite8_x, sprite8_y, sprite8_w, sprite8_h,sprite11_x, sprite11_y, sprite11_w, sprite11_h )==1){
o1.n=1;
score=score+100;
f--;
if (f==0) {
o1.x=-10000000;
o1.y=-10000000;
f=8;
}
k.x=c.x;
}

if(bounding_box_collision(sprite9_x, sprite9_y, sprite9_w, sprite9_h,sprite11_x, sprite11_y, sprite11_w, sprite11_h )==1){
o2.n=1;
score=score+100;
if (f==0) {
o2.x=-10000000;
o2.y=-10000000;
f=8;
}
k.x=c.x;
}

if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,sprite6_x, sprite6_y, sprite6_w, sprite6_h )==1){
m1.n=1;
score=score+20;
m1.x=-100;
m1.y=-100;
}

if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,sprite7_x, sprite7_y, sprite7_w, sprite7_h )==1){
m2.n=1;
score=score+20;
m2.x=-100;
m2.y=-100;
}

if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,sprite3_x, sprite3_y, sprite3_w, sprite3_h )==1){
e.n=1;
score=score+30;
e.x=-100;
e.y=-100;
}

if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,sprite4_x, sprite4_y, sprite4_w, sprite4_h )==1){
e1.n=1;
score=score+30;
e1.x=-100;
e1.y=-100;
}

if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,sprite5_x, sprite5_y, sprite5_w, sprite5_h )==1){
e2.n=1;
score=score+30;
e2.x=-100;
e2.y=-100;
}


if(o.n==0){

if (o.x>800 && test ==1)
{
draw_sprite(Buffer, o.tab_g[0], o.x, o.y);
o.x--;
}else
{
draw_sprite(Buffer, o.tab_g[0], o.x, o.y);
o.x++;
}
if (o.x<=800)
test =0;
if (o.x>1000)
test =1;
}

if(o1.n==0)
{
if (o1.x>9000 && test ==1)
{
draw_sprite(Buffer, o1.tab_g[0], o1.x, o1.y);
o1.x--;
}else
{
draw_sprite(Buffer, o1.tab_g[0], o1.x, o1.y);
o1.x++;
}
if (o1.x<=9000)
test =0;
if (o1.x>8000)
test =1;
}

if(o2.n==0){
draw_sprite(Buffer, o2.tab_g[0], o2.x, o2.y);
o2.x--;
}

if(m.n==0){
draw_sprite(Buffer, m.money, m.x, m.y);
}

if(m1.n==0){
draw_sprite(Buffer, m.money, m1.x, m1.y);
}

if(m2.n==0){
draw_sprite(Buffer, m.money, m2.x, m2.y);
}

if(e.n==0){
draw_sprite(Buffer, e.etoile, e.x, e.y);
}

if(e1.n==0){
draw_sprite(Buffer, e.etoile, e1.x, e1.y);
}

if(e2.n==0){
draw_sprite(Buffer, e.etoile, e2.x, e2.y);
}

if (b.n==0) 
{
draw_sprite(Buffer, b.tab_d[0], b.x, b.y);
b.y--;
}

if (key[KEY_RIGHT] && c.x<1300 && collision_Background_right(poz,c,Background2)==0) 
{
compteur++;
c.x=c.x+0.5;
c.direction=0;
if(poz<14142-1300)
{poz+=7;
e.x-=7;
m.x-=7;
e1.x-=7;
b.x-=7;
m1.x-=7;
e2.x-=7;
m2.x-=7;
//o.x-=7;
o1.x-=7;
o2.x-=7;
}
}
if (key[KEY_LEFT] && c.x > 0 && collision_Background_left(poz,c,Background2)==0) {
compteur++;
c.x=c.x-0.5;
c.direction=1;
if(poz>0)
{
poz-=7;
e.x+=7;
//o.x+=7;
o1.x+=7;
o2.x+=7;
b.x+=7;
m.x+=7;
e1.x+=7;
m1.x+=7;
e2.x+=7;
m2.x+=7;}
}
if (key[KEY_Z] )
kar=1;
if (key[KEY_X] )
{kar=0;
k.x=c.x;}

if (kar==1 )
{if (c.direction ==0)
{ if (k.x<SCREEN_W)
{draw_sprite(Buffer, k.tab_d[0], k.x, c.y);
k.x+=15;}
else
k.x=c.x;
}
else
{draw_sprite(Buffer, k.tab_d[0], k.x, c.y);
k.x-=15;}}

if (c.direction==0)
{

draw_sprite(Buffer, c.tab_d[compteur%3], c.x, c.y);//dessiner l'image de personnage sur le buffer

} 
else{

draw_sprite(Buffer, c.tab_g[compteur%3], c.x, c.y);
}

blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);//copier le buffer sur la fenaitres

clear_bitmap(Buffer);

if(compteur==1000)
{
compteur=0;
}
}

destroy_bitmap(Buffer);

blit( Buffer,screen, 0, 0, 0, 0, 1300,700);

return 0;
}

END_OF_MAIN();

int bounding_box_collision(int sprite1_x, int sprite1_y, int sprite1_w, int sprite1_h, int
sprite2_x, int sprite2_y, int sprite2_w, int sprite2_h)
{
if ((sprite1_x > sprite2_x + sprite2_w - 1) || // is sprite1 on the right side of sprite2?
(sprite1_y > sprite2_y + sprite2_h - 1) || // is sprite1 under sprite2?
(sprite2_x > sprite1_x + sprite1_w - 1) || // is sprite2 on the right side of sprite11?
(sprite2_y > sprite1_y + sprite1_h - 1)) // is sprite2 under sprite11?
{
// no collision
return 0;
}
else
// collision
return 1;
}

void timer_routine(int timer)
{
 timer++;
}

int get_pixel(BITMAP* Buffer,int x,int y)
{
	int tmp;
	tmp=getpixel(Buffer,x,y);
	return((getr(tmp) + getb(tmp) + getg(tmp)) / 3);
}


void gravity(float *y)
{   
	*y+=3;
}

void gravity_mario(float *poz,codo *c,BITMAP * Background)
{

	int sprite1_x=c->x;
	int sprite1_y=c->y;
	int sprite1_w=c->tab_d[0]->w;
	int sprite1_h=c->tab_d[0]->h;

	if(!collision_Background_down(poz,c->x,c->y,c->tab_d[0]->h,c->tab_d[0]->w,Background))
		gravity(&(c->y));
}


int collision_Background_down(float *poz,int sprite1_x,int sprite1_y,int sprite1_h,int sprite1_w,BITMAP* Background2)
{
	int x;
	int y;
	x=sprite1_x + (sprite1_w /2);
	y=sprite1_y + sprite1_h;

	if( get_pixel(Background2,(*poz)+x,y)== 0)
		return (1); //collision with the middle down side of the sprite
else
	return (0); //no collision

}

int collision_Background_up(float *poz,codo *c,int sprite1_x,int sprite1_y,int sprite1_h,int sprite1_w,BITMAP* Background2)
{
	int x;
	int y;
	x=sprite1_x + (sprite1_w /2);
	y=sprite1_y-sprite1_h;

	if( get_pixel(Background2,(*poz)+c->x,c->y)== 0)
		return (1); //collision with the middle left side of the sprite
else
	return (0); //no collision

}

int collision_Background_left(float poz,codo c,BITMAP* Background2)
{
	int x;
	int y;
	x=c.x;
	y=c.y+(c.tab_d[0]->h/2);

	if( get_pixel(Background2,(poz)+x,y)== 0)
		return (1); //collision with the middle left side of the sprite
else
	return (0); 
}

int collision_Background_right(float poz,codo c,BITMAP* Background2)
{
	int x;
	int y;
	x=c.x + c.tab_d[0]->w;
	y=c.y+(c.tab_d[0]->h/2);

	if( get_pixel(Background2,(poz)+x,y)== 0)
		return (1); //collision with the middle left side of the sprite
else
	return (0);
}

void jump_mario(float *poz,codo *c,BITMAP * Background)
{
	int sprite1_x=c->x;
	int sprite1_y=c->y;
	int sprite1_w=c->tab_d[2]->w;
	int sprite1_h=c->tab_d[2]->h;

	if (key[KEY_UP] && (c->y > -20) && !collision_Background_up(poz,c,c->x,c->y,c->tab_d[2]->h,c->tab_d[2]->w,Background) ) 
{

c->y-=6;
}			
}

void move_mario(float *poz,codo *c,BITMAP * Background)
{
	jump_mario(poz,c,Background);
	gravity_mario(poz,c,Background);
	//temp(mario);
}

